﻿//javascript functions of set relation dialog-box

//get new and to remove relation grid data a/c to rule
function getRelationData(node_id, dimname, table_name, hier_id, txt_date, queryRel) {
    //"dtgrid${dimabbr}_rule"
    ////debugger;
//    var fl_rel = $("#dtgrid" + dimname + "_rule");
//    var queryRel = fl_rel.jqFilter('toSQLString');

//    alert("#dtgrid" + dimname + "_rule");
//    alert(fl_rel);
    //alert(queryRel);
    // //debugger;

    queryRel = queryRel.split("\"").join("'");

    var urlNew = "Callback.aspx?operation=getRelDataNew&node_id=" + node_id + "&dimname=" + dimname + "&hier_id=" + hier_id + "&queryRel=" + queryRel + "&table_name=" + table_name + "&txtdate=" + txt_date;
    var urlRem = "Callback.aspx?operation=getRelDataRem&node_id=" + node_id + "&dimname=" + dimname + "&hier_id=" + hier_id + "&queryRel=" + queryRel + "&table_name=" + table_name + "&txtdate=" + txt_date;

    //getRelColData
    $.ajax({
        async: true,
        type: 'POST',
        url: "Callback.aspx?operation=getRelColData&node_id=" + node_id + "&dimname=" + dimname + "&hier_id=" + hier_id + "&queryRel=" + queryRel + "&table_name=" + table_name + "&txtdate=" + txt_date,
        success: function (str) { //griddata

            ////debugger;
            //alert("kk");
            var result = eval('(' + str + ')');
            //   newgriddata.colModelList = listColmodel;
            // newgriddata.columns = columns;

            result.remDimGrid.colModelList = result.newDimGrid.colModelList;
            result.remDimGrid.columns = result.newDimGrid.columns;

            fillgrid(result.newDimGrid, "#dtgridNew", "#pagerNew", "800px", "150px", urlNew,true); //dtgridNew
            fillgrid(result.remDimGrid, "#dtgridRem", "#pagerRem", "800px", "150px", urlRem,true);

            jQuery("#dtgridNew").jqGrid('setCaption', 'New master data to add');
            jQuery("#dtgridRem").jqGrid('setCaption', 'Master data to remove');

            jQuery("#dtgridNew").jqGrid('navGrid', '#pagerNew', { edit: false, add: false, del: false, search: true });
            jQuery("#dtgridRem").jqGrid('navGrid', '#pagerRem', { edit: false, add: false, del: false, search: true });

        }
    });
}

//add relation of selected dimensions to selected node 
function addRelationRule() {

    ////debugger;
    var dimname = $("#hid_dimname").val();
    //add the relation to that node
    add_relaionRule(dimname, "#dtgridNew");
}

////add the relation of dimension
function add_relaionRule(dimname, tblgrid) {
    ////debugger;

    var Ids = jQuery(tblgrid).getGridParam('selarrrow');
    var node_id = $("#hid_NodeId").val();
    var table_name = $("#hid_TableName").val();
    var hier_id = $("#hid_HierarchyId").val();
    var vers_id = $("#hid_versid").val();
    var txtdate = $("#hid_txtdate").val();
    var queryRel = $("#hid_query").val();

   // var queryRel = window.opener.$("#div_" + dimname + "currule").html();

    queryRel = queryRel.split("\"").join("'");
    var urlNew = "Callback.aspx?operation=getRelDataNew&node_id=" + node_id + "&dimname=" + dimname + "&hier_id=" + hier_id + "&queryRel=" + queryRel + "&table_name=" + table_name + "&txtdate=" + txtdate;

    if (Ids != undefined) {
        if (Ids.length != 0 && node_id != undefined) {
            $.ajax({
                async: true,
                type: 'POST',
                url: "Callback.aspx?operation=add_relRule&dimname=" + dimname + "&hier_id=" + hier_id+ "&table_name=" + table_name + "&queryRel=" + queryRel + "&node_id=" + node_id + "&Ids=" + Ids + "&vers_id=" + vers_id + "&txtdate=" + txtdate,
                success: function (data) {
                    ////debugger;
                   // var result = eval('(' + data + ')');
                    //refreshgrid(result, "#dtgridNew");

                    //if added the relation setting boolean to refresh 
                    //exist and unassigned grid
                    $("#hid_ret").val('true');

                    /////////
                    //var result = eval('(' + str + ')');
                    //   newgriddata.colModelList = listColmodel;
                    // newgriddata.columns = columns;      
                    //result.remDimGrid.colModelList = result.newDimGrid.colModelList;
                   // result.remDimGrid.columns = result.newDimGrid.columns;

                   // refreshgrid(result.newDimGrid, "#dtgridNew", urlNew); //dtgridNew
                    refreshgrid(data, "#dtgridNew", urlNew); //dtgridNew
                    //refreshgrid(result.remDimGrid, "#dtgridRem");

                    ///////////


                }
            });
        }
        else {
            alertmsgAddRel(Ids, node_id);
        }
    }
    else {
        alertmsgAddRel(Ids, node_id);
    }
}


//function closeDlg() {
//    //flag for refresh of exist and un-assgined grid,
//    //if we have added relation
//    alert('l');
//  //debugger;
//    window.returnValue = $("#hid_ret").val();
//    //window.close();
//}

//function closeDlgCall() {
//  //debugger;
//    alert('l');
//    //flag for refresh of exist and un-assgined grid,
//    //if we have added relation
//    window.close();
//}


//remove the relation of existing data
function remove_relRule() {

    ////debugger;

    var dimname = $("#hid_dimname").val();
    var tblgrid = "#dtgridRem";

    var Ids = jQuery(tblgrid).getGridParam('selarrrow');
    var node_id = $("#hid_NodeId").val();
    var table_name = $("#hid_TableName").val();
    var hier_id = $("#hid_HierarchyId").val();
    var vers_id = $("#hid_versid").val();
    var txtdate = $("#hid_txtdate").val();
    var queryRel = $("#hid_query").val();
   // var queryRel = window.opener.$("#div_" + dimname + "currule").html();

    queryRel = queryRel.split("\"").join("'");

    var urlRem = "Callback.aspx?operation=getRelDataRem&node_id=" + node_id + "&dimname=" + dimname + "&hier_id=" + hier_id + "&queryRel=" + queryRel + "&table_name=" + table_name + "&txtdate=" + txtdate;

    if (Ids != undefined) {
        if (Ids.length != 0 && node_id != undefined) {

            $.ajax({
                async: true,
                type: 'POST',
                //url: "Callback.aspx?operation=remove_rel&hier_id=" + $("#drp_hier").val(),
                url: "Callback.aspx?operation=remove_relRule&table_name=" + table_name + "&dimname=" + dimname + "&queryRel=" + queryRel + "&hier_id=" + hier_id + "&node_id=" + node_id + "&Ids=" + Ids + "&txtdate=" + txtdate,
                success: function (data) { //griddata
                    ////debugger;
                    //exist and unassigned grid
                    $("#hid_ret").val('true');
                    refreshgrid("data", "#dtgridRem", urlRem);

                },
                loadError: function (xhr, status, error) {
                    ////debugger;
                    alert('error');
                }

            });

        }
        else {
            alert("Please select dimension to remove relation");
            return false;
        }
    }
    else {
        alert("Please select dimension to remove relation");
        return false;
    }

}